package com.sgb.goods.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.goods.R;
import com.swgk.core.dialog.RoundCornerDialog;

/**
 * @author younghow
 * @date 2020/10/29
 * description：针对与dialog的一些通用设置
 * first:写在前面，我们在命名的时候一定采用驼峰的方式
 * second:在命名的时候一定要做到代码自解释，也就是说我们用翻译就大概知道这个是要干嘛的
 */
public class DialogHelper {

    public interface DialogFunction {

        default void normalRoundDialog() {
        }
    }

    /**
     * 弹出框设置,罪通常的dialog，分为标题，内容，左右案件，圆角
     *
     * @param context        上下文
     * @param title          弹出框标题
     * @param message        弹出框内容
     * @param positiveStr    确定按钮
     * @param dialogFunction 确定键回调
     */
    public static void showDialog(Context context, String title, String message, String positiveStr, DialogFunction dialogFunction) {
        View view = View.inflate(context, R.layout.goods_dialog_two_btn_circle, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);

        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvMessage = view.findViewById(R.id.tv_message);
        TextView tvLogoutConfirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tvLogoutCancel = view.findViewById(R.id.tv_logout_cancel);
        if (title != null) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        } else {
            tvTitle.setVisibility(View.GONE);
        }

        if (message != null) {
            tvMessage.setVisibility(View.VISIBLE);
            tvMessage.setText(message);
        } else {
            tvMessage.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(positiveStr)) {
            tvLogoutConfirm.setText(positiveStr);
        }
        //确定
        tvLogoutConfirm.setOnClickListener(v -> {
            if(null != dialogFunction) {
                dialogFunction.normalRoundDialog();
            }
            roundCornerDialog.dismiss();

        });
        //取消
        tvLogoutCancel.setOnClickListener(v -> roundCornerDialog.dismiss());
    }

    /**
     * 文字提示Dialog
     *
     * @param context               上下文
     * @param title                 标题
     * @param contentText           提示文字
     * @param CancelString          取消按钮文字
     * @param confirmString         确定文字
     * @param dialogTextReturnValue 点击确定回调
     */
    public static void showDialog(Context context, String title, String contentText, String CancelString, String confirmString, DialogFunction dialogTextReturnValue) {
        showDialog(context, title, contentText, CancelString, null, confirmString, dialogTextReturnValue);
    }

    /**
     * 文字提示Dialog
     *
     * @param context               上下文
     * @param title                 标题
     * @param contentText           提示文字
     * @param CancelString          取消按钮文字
     * @param dialogTextReturnValue 点击取消回调
     * @param confirmString         确定文字
     * @param dialogTextReturnValue 点击确定回调
     */
    public static void showDialog(Context context, String title, String contentText, String CancelString, DialogFunction cancelDialogFunction, String confirmString, DialogFunction dialogTextReturnValue) {
        View view = View.inflate(context, R.layout.goods_dialog_showtexttip, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);

        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvLogoutConfirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tvLogoutCancel = view.findViewById(R.id.tv_logout_cancel);
        TextView tvContent = view.findViewById(R.id.tv_content);
        View view_lin = view.findViewById(R.id.view_lin);

        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
        }else{
            tvTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(contentText)) {
            tvContent.setText(contentText);
        }
        if (!TextUtils.isEmpty(CancelString)) {
            tvLogoutCancel.setVisibility(View.VISIBLE);
            view_lin.setVisibility(View.VISIBLE);
            tvLogoutCancel.setText(CancelString);
        } else {
            tvLogoutCancel.setVisibility(View.GONE);
            view_lin.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(confirmString)) {
            tvLogoutConfirm.setText(confirmString);
        }
        tvLogoutConfirm.setOnClickListener(v -> {
            roundCornerDialog.dismiss();
            if(null != dialogTextReturnValue) {
                dialogTextReturnValue.normalRoundDialog();
            }
        });

        //取消
        tvLogoutCancel.setOnClickListener(v -> {
            roundCornerDialog.dismiss();
            if(null != cancelDialogFunction) {
                cancelDialogFunction.normalRoundDialog();
            }
        });

    }
}
